package com.yanglei.client;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Window;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClientLogin {

	private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="206,24"
	private JPanel jPanel1 = null; // @jve:decl-index=0:visual-constraint="551,14"
	private JPanel jPanel2 = null; // @jve:decl-index=0:visual-constraint="636,237"
	private JPanel jPanel3 = null; // @jve:decl-index=0:visual-constraint="520,113"
	private JPanel jPanel4 = null;
	private JPanel jPanel5 = null;// @jve:decl-index=0:visual-constraint="235,268"
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	private JLabel jLabel1 = null;
	private JTextField username = null;
	private JLabel jLabel2 = null;
	private JTextField hostAddress = null;
	private JLabel jLabel3 = null;
	private JTextField port = null;

	public JFrame getjFrame() {
		return jFrame;
	}

	public ClientLogin() {
		jFrame = new JFrame("客户端登陆");
		init();
	}

	public void init() {
		jPanel1 = new JPanel();
		jPanel2 = new JPanel();
		jPanel3 = new JPanel();
		jPanel4 = new JPanel();
		jPanel5 = new JPanel();

		jButton1 = new JButton("登陆");
		jButton2 = new JButton("重置");

		jButton1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				ClientLogin.this.login(e);
			}
		});

		jLabel1 = new JLabel("用户名");
		jLabel2 = new JLabel("服务器");
		jLabel3 = new JLabel("端口号");

		username = new JTextField(15);
		hostAddress = new JTextField(15);
		port = new JTextField(15);

		username.setText("杨磊");
		hostAddress.setText("127.0.0.1");
		port.setText("9988");

		GridLayout gridlayout = new GridLayout(3, 1);

		FlowLayout flowlayout = new FlowLayout();

		jPanel1.setLayout(flowlayout);
		jPanel1.add(jLabel1);
		jPanel1.add(username);

		jPanel5.setLayout(flowlayout);
		jPanel5.add(jLabel3);
		jPanel5.add(port);

		jPanel2.setLayout(flowlayout);
		jPanel2.add(jLabel2);
		jPanel2.add(hostAddress);

		jPanel3.setLayout(gridlayout);

		jPanel3.add(jPanel1);
		jPanel3.add(jPanel2);
		jPanel3.add(jPanel5);

		jPanel4.setLayout(flowlayout);
		jPanel4.add(jButton1);
		jPanel4.add(jButton2);

		jButton2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				username.setText("");
				hostAddress.setText("");
				port.setText("");
			}
		});

		BorderLayout borderlayout = new BorderLayout();
		jFrame.setLayout(borderlayout);

		jFrame.add(jPanel3, borderlayout.CENTER);
		jFrame.add(jPanel4, borderlayout.SOUTH);

		jFrame.setVisible(true);
		jFrame.setLocation(500, 250);
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.pack();
	}

	private void login(ActionEvent e) {
		String username = this.username.getText();
		String hostAddress = this.hostAddress.getText();
		int port = Integer.parseInt(this.port.getText());

		ClientConnection cc = new ClientConnection(this, hostAddress, port,
				username);
		if (cc.login()) {
			cc.start();
		} else {
			JOptionPane.showMessageDialog(this.getjFrame(), "用户名重复", "错误",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	public static void main(String[] args) {
		new ClientLogin();
	}
}
